# coding=utf-8
import maya.mel as mel
import maya.cmds as cmds
#import core as timelimit
class GraphEditor_Win () :
	
	def ShowFilters(self):
		mel.eval('filterUIClearFilter graphEditor1OutlineEd')
		
		#selectionList = cmds.ls(selection=1)
		#for sl in selectionList:
		#	attributes = cmds.attributeInfo(allAttributes=1)
		#	for attribute in attributes:
		#		if cmds.attributeQuery(attribute, node=sl, keyable=True):
		#			mel.eval('filterUISelectAttributesCheckbox' + ' ' + attribute + ' 1 graphEditor1OutlineEd;')
		
		
	def TX(self):
		mel.eval('filterUISelectAttributesCheckbox translateX 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''

	def TY(self):
		#mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def TZ(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox translateZ 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def RX(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateX 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def RY(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateY 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def RZ(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox rotateZ 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def SX(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleX 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def SY(self):
		'''
		mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleY 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
	def SZ(self):
		'''mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox translateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateY 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox rotateZ 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleX 0 graphEditor1OutlineEd;')
		mel.eval('filterUISelectAttributesCheckbox scaleY 0 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox scaleZ 1 graphEditor1OutlineEd;')
		'''
		mel.eval('filterUISelectAttributesCheckbox visibility 0 graphEditor1OutlineEd;')
		'''
		
	def getOffsetAmount(self):		
		value= cmds.textField(	self.OffsetTF ,q=1,text=1)
		intVal =  int(value)
		cmds.keyframe (edit=1,relative=1, timeChange =intVal)
	def getNegOffsetAmount(self):
		value= '-' + cmds.textField(	self.OffsetTF ,q=1,text=1)
		intVal =  int(value)
		cmds.keyframe (edit=1,relative=1, timeChange =intVal)

	def SmoothKeys(self):
		selected_anim_curves = cmds.keyframe(query=True, name=True)
		try:
			len(selected_anim_curves)
		except BaseException:
			cmds.error(u'请至少选择一个有动画的物体,或者动画曲线!')

		for each_curve in selected_anim_curves:

			keyframes_selected = cmds.keyframe(each_curve, query=True, time=(), selected=True)
			try:
				len(keyframes_selected)
			except BaseException:
				keyframes = cmds.keyframe(each_curve, query=True, time=())
			else:
				keyframes = cmds.keyframe(each_curve, query=True, time=(), selected=True)
			# print keyframes
			# else:
			# keyframes = cmds.keyframe(each_curve, query=True, time=(),selected=True)
			# print keyframes

			if len(keyframes) < 3:
				keys_count = str(len(keyframes))
				# print each_curve+string_utf8+keys_count+string2_utf8
				print(each_curve + u' 只有或者只选择了' + keys_count + u'帧动画,跳过此曲线(至少需要三帧动画).')
				continue

			else:
				# print each_curve+': '+str(keyframes)

				duplicated_anim_curve = cmds.duplicate(each_curve, name=each_curve + '_temp')

				index = 1
				while index < len(keyframes) - 1:
					# print keyframes[index]

					last_frame_value = cmds.keyframe(each_curve, query=True,
													 time=(keyframes[index - 1], keyframes[index - 1]),
													 valueChange=True)
					print(last_frame_value)

					current_frame_value = cmds.keyframe(each_curve, query=True,
														time=(keyframes[index], keyframes[index]),
														valueChange=True)
					# print current_frame_value

					next_frame_value = cmds.keyframe(each_curve, query=True,
													 time=(keyframes[index + 1], keyframes[index + 1]),
													 valueChange=True)
					# print next_frame_value

					average_value = (last_frame_value[0] + current_frame_value[0] + next_frame_value[0]) / 3
					# duplicated_anim_curve = cmds.duplicate[each_curve,name=each_curve+'_temp']

					cmds.keyframe(duplicated_anim_curve, absolute=True, time=(keyframes[index], keyframes[index]),
								  valueChange=average_value)
					index += 1

				index = 1
				while index < len(keyframes) - 1:
					current_frame_value_duplicate_curve = cmds.keyframe(duplicated_anim_curve, query=True,
																		time=(keyframes[index], keyframes[index]),
																		valueChange=True)
					current_frame_value_orig_curve = cmds.keyframe(each_curve, query=True,
																   time=(keyframes[index], keyframes[index]),
																   valueChange=True)
					deviation_value = current_frame_value_orig_curve[0] - current_frame_value_duplicate_curve[0]
					# print D-value
					#smooth_strength = cmds.floatSliderGrp('floatSlider_MDF', query=True, v=True)
					final_value = current_frame_value_orig_curve[0] - deviation_value * 1

					cmds.keyframe(each_curve, absolute=True, time=(keyframes[index], keyframes[index]),
								  valueChange=final_value)
					index += 1

				cmds.delete(duplicated_anim_curve)

	def curveSelection(self,chosenAxis):
		mel.eval("filterUISelectAttributesCheckbox " +  chosenAxis +  " 1 graphEditor1OutlineEd;")

		shuxing = []
		shuxing.append(chosenAxis)
		toggleCounter = ['translateX','translateY','translateZ','rotateX','rotateY','rotateZ','scaleX','scaleY','scaleZ']
		shengyu_shuxing = set(toggleCounter) -set(shuxing)


		for chosenAxi in shengyu_shuxing:
			mel.eval("filterUISelectAttributesCheckbox " +  chosenAxi +  " 0 graphEditor1OutlineEd;")

			#if chosenAxis in chosenAxi:
			#	mel.eval("filterUISelectAttributesCheckbox " +  chosenAxis +  " 1 graphEditor1OutlineEd;")
			#	print '1111111111'
			#else:
			#	mel.eval("filterUISelectAttributesCheckbox " +  chosenAxis +  " 0 graphEditor1OutlineEd;")
			#	print '22222222222'
		#mel.eval('filterUISelectAttributesCheckbox translateX 0 graphEditor1OutlineEd;')


	def GraphEditor_Win(self):
		#if timelimit.ProductActivation().Win() == 0:
		#	return
		TSLength = cmds.optionVar (q= "timeSliderLength")
		#graphEditorWin = cmds.window('graphEditorPlusWindow', exists=1)
		#if graphEditorWin:#graphEditorPlusWindow
		#	#cmds.window(graphEditorWin,e = 1,closeCommand=1)
		#	cmds.setFocus ('graphEditorPlusWindow')
		#	#cmds.deleteUI ('graphEditorPlusWindow')
		#else:
		graphEditorWin = 'graphEditorPlusWindow'
		if cmds.window(graphEditorWin, exists=1):

			cmds.deleteUI(graphEditorWin)
		cmds.window (graphEditorWin,title ="Graph Editor" )
		form =cmds.formLayout()

		pane = cmds.paneLayout(configuration ="single",h = 300)
		cmds.setParent ( '..' )

		row = cmds.rowLayout(numberOfColumns =18,adjustableColumn=17)#,adjustableColumn=2
		cmds.button(l='TX',w=20,backgroundColor= [1,0,0],command = lambda *args :self.curveSelection('translateX'))
		cmds.button(l='TY',w=20,backgroundColor= [0,1,0],command = lambda *args :self.curveSelection('translateY'))
		cmds.button(l='TZ',w=20,backgroundColor= [1,0.254902,0.6],command = lambda *args :self.curveSelection('translateZ'))
		cmds.button(l='RX',w=20,backgroundColor= [1,0,0],command = lambda *args :self.curveSelection('rotateX'))
		cmds.button(l='RY',w=20,backgroundColor= [0,1,0],command = lambda *args :self.curveSelection('rotateY'))
		cmds.button(l='RZ',w=20,backgroundColor= [1,0.254902,0.6],command = lambda *args :self.curveSelection('rotateZ'))
		cmds.button(l='SX',w=20,backgroundColor= [1,0,0],command = lambda *args :self.curveSelection('scaleX'))
		cmds.button(l='SY',w=20,backgroundColor= [0,1,0],command = lambda *args :self.curveSelection('scaleY'))
		cmds.button(l='SZ',w=20,backgroundColor= [1,0.254902,0.6],command = lambda *args :self.curveSelection('scaleZ'))
		cmds.button(l='Show',w=35,backgroundColor= [1,1,1],command = lambda *args :self.ShowFilters())
		cmds.text(l='---------',vis=0,w=30,h=30)
		cmds.button(l='< --',w=30,h=30,command = lambda *args :self.getNegOffsetAmount(),backgroundColor= [1,0,0])
		self.OffsetTF =cmds.textField(w=30,h=30)
		cmds.button(l='-- >',w=30,h=30,command = lambda *args :self.getOffsetAmount(),backgroundColor= [0,1,0])
		cmds.text(l='---------',vis=0,w=30,h=30)
		cmds.button(l='10', w=70, h=30, command=lambda *args: self.SmoothKeys(), backgroundColor=[0, 1, 0])
		cmds.timePort (bgc =[0.4 ,0.4, 0.4],h =30)#,w = TSLength
		cmds.setParent ( '..' )

		cmds.formLayout(form, e=1, af=[(row, 'left', 5), (row, 'right', 5), (row, 'bottom', 5), (pane, 'top', 5),(pane, 'left', 5), (pane, 'right', 5) ], attachControl=[(pane, 'bottom', 4, row)] )
		GraphEditors = cmds.getPanel( scriptType= 'graphEditor')
		if len(GraphEditors):
			cmds.scriptedPanel (GraphEditors[0],e=1, unParent=1)
		else:
			cmds.scriptedPanel ('graphEditor',unParent =1, l="Graph Editor")
		cmds.scriptedPanel (GraphEditors[0],e=1 ,parent = pane)
		cmds.setFocus ('graphEditorPlusWindow')
		#print 'ssssssssssssssssssssssssssssss'
		#print '1111111111111111111111111111111'
		cmds.showWindow(graphEditorWin)
#GraphEditor_Win ().GraphEditor_Win ()